﻿using System.IO;
using System.Runtime.Serialization;
using System.Text;

namespace nRoute.Samples.Sudoku.Components
{
    /// <remarks>
    /// Taken from http://www.ingebrigtsen.info/post/2008/11/29/Serialization-in-Silverlight.aspx
    /// and http://www.willasrari.com/blog/json-serialization-with-silverlight-isolated-storage-for-the-win/000322.aspx
    /// </remarks>
    public static class Serializer
    {
        public static string Serialize<T>(T data)
        {
            try
            {
                using (var _stream = new MemoryStream())
                {

                    var _serializer = new DataContractSerializer(typeof(T));
                    _serializer.WriteObject(_stream, data);

                    _stream.Seek(0, SeekOrigin.Begin);

                    var reader = new StreamReader(_stream);
                    string content = reader.ReadToEnd();
                    return content;

                }
            }
            catch (InvalidDataContractException)
            {
                return string.Empty;
            }
        }

        public static T Deserialize<T>(string xml)
        {
            try
            {
                using (var _stream = new MemoryStream(Encoding.Unicode.GetBytes(xml)))
                {

                    var _serializer = new DataContractSerializer(typeof(T));
                    return (T)_serializer.ReadObject(_stream);

                }
            }
            catch (SerializationException)
            {
                return default(T);
            }

        }

        // REQUIRES Reference to System.ServiceModel.Web

        //public static string JSONSerialize<T>(T input)
        //{
        //    try
        //    {
        //        using (var _stream = new MemoryStream())
        //        {

        //            var _serializer = new DataContractJsonSerializer(input.GetType());
        //            _serializer.WriteObject(_stream, input);
        //            _stream.Position = 0;

        //            using (var _reader = new StreamReader(_stream)) return _reader.ReadToEnd();

        //        }
        //    }
        //    catch (InvalidDataContractException)
        //    {
        //        return string.Empty;
        //    }
        //}

        //public static T JSONDeserialize<T>(string json)
        //{
        //    try
        //    {
        //        using (var _stream = new MemoryStream())
        //        {

        //            byte[] bytes = Encoding.Unicode.GetBytes(json);
        //            _stream.Write(bytes, 0, bytes.Length);
        //            _stream.Position = 0;
        //            var _serializer = new DataContractJsonSerializer(typeof(T));

        //            return (T)_serializer.ReadObject(_stream);

        //        }
        //    }
        //    catch (SerializationException)
        //    {
        //        return default(T);
        //    }
        //}

    }
}
